package Loganseabolt.blocks;

import java.util.Random;

import Loganseabolt.LoganSeaboltModBase;

import net.minecraft.block.Block;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.ForgeDirection;

public class TomatoCrop extends BlockFlower
{
        public TomatoCrop(int par1) {
                super(par1,Material.plants);
                
                this.setBlockBounds(0, 0, 0, 1, 1.5f, 1);
            this.setCreativeTab(CreativeTabs.tabBlock);
           
        
                // TODO Auto-generated constructor stub
        }
        public TomatoCrop(int par1, Material par2Material) {
                super(par1, par2Material);
                

        this.setCreativeTab(CreativeTabs.tabBlock);
                // TODO Auto-generated constructor stub
        }
                             
        public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
        {
                return null;
        }
    
        public int getRenderType()
        {
                return 6;
        }
        public boolean isOpaqueCube()
        {
                return false;
        }
    public void registerIcons(IconRegister iconReg)
    {
        blockIcon = iconReg.registerIcon("loganSeaboltModBase:tomatoCrop");
    }
    
    public int getBlockTextureFromSideAndMetadata (int side, int metadata)
    {
        return 32 + metadata;
    }
    
    public void updateTick( World world, int x, int y, int z, Random random)
    {
        if (world.getBlockMetadata(x, y, z)== 1)
        {
                return;
        }
        if (random.nextInt(isFertile(world, x, y-1, z)? 12:25) != 0)
        {
                return;
        }
        world.setBlockMetadataWithNotify(x, y, z, 1,0);
    }
    
    public void onNeighborChange(World world, int x, int y, int z, int neighborId)
    {
        if (!canBlockStay(world, x,y,z))
        {
                dropBlockAsItem(world, x, y, z, world.getBlockMetadata(x, y, z), 0);
                world.setBlock(x, y, z, 0);
                
        }
    }
    public boolean canBlockStay(World world, int x, int y, int z)
    {
        Block soil = blocksList[world.getBlockId(x, y-1, z)];
        return (world.getFullBlockLightValue(x, y, z) >= 8 || world.canBlockSeeTheSky(x, y, z)) && (soil != null && soil.canSustainPlant(world, x, y-1, z, ForgeDirection.UP,LoganSeaboltModBase.tomatoSeeds)); 
                        
    
    }
    
    public int idDropped(int metadata, Random r, int p)
    {
        switch (metadata)
        {
        case 0:
                return LoganSeaboltModBase.tomatoSeeds.itemID;
        case 1:
                return LoganSeaboltModBase.Tomato.itemID;
        case 2:
            return LoganSeaboltModBase.Souls.itemID;
        default :
                return -1;
                
        }
    }
    
    public int idPicked(World world, int x, int y, int z)
    {
        return LoganSeaboltModBase.tomatoSeeds.itemID;
    }
    @Override
    public EnumPlantType getPlantType(World world, int x, int y, int z)
    {
        if (blockID == crops.blockID        ) return EnumPlantType.Crop;
        if (blockID == deadBush.blockID     ) return EnumPlantType.Desert;
        if (blockID == waterlily.blockID    ) return EnumPlantType.Water;
        if (blockID == mushroomRed.blockID  ) return EnumPlantType.Cave;
        if (blockID == mushroomBrown.blockID) return EnumPlantType.Cave;
        if (blockID == netherStalk.blockID  ) return EnumPlantType.Nether;
        if (blockID == sapling.blockID      ) return EnumPlantType.Plains;
        if (blockID == melonStem.blockID    ) return EnumPlantType.Crop;
        if (blockID == pumpkinStem.blockID  ) return EnumPlantType.Crop;
        if (blockID == tallGrass.blockID    ) return EnumPlantType.Plains;
        return EnumPlantType.Plains;
    }

    @Override
    public int getPlantID(World world, int x, int y, int z)
    {
        return blockID;
    }

    @Override
    public int getPlantMetadata(World world, int x, int y, int z)
    {
        return world.getBlockMetadata(x, y, z);
    }

}
